package com.dayuanit.booksystem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {

    //key=属性的名字 value=表字段的名字
    public static final Map<String, String> bookMap = new HashMap<>();
    public static final Map<String, String> publisherMap = new HashMap<>();

    //book_id as id, book_name as bookName, author as author, price as price, publisher_id as publisherId
    //publisher_id as id, publisher_name as name, publisher_address as address
    static {
        bookMap.put("id", "book_id");
        bookMap.put("bookName", "book_name");
        bookMap.put("author", "author");
        bookMap.put("price", "price");
        bookMap.put("publisherId", "publisher_id");

        publisherMap.put("id", "publisher_id");
        publisherMap.put("name", "publisher_name");
        publisherMap.put("address", "publisher_address");
    }

    @Override
    public void addPublisher(String name, String address) {
        String sql = "insert into publisher(publisher_name, publisher_address) values('%s', '%s')";
        sql = String.format(sql, name, address);
        //执行sql语句，Steament
        DataBaseUtil.modify(sql);
    }

    @Override
    public Publisher queryPublisher(String publisherName) {
        String sql = "SELECT * from publisher WHERE publisher_name='%s'; ";
        sql = String.format(sql, publisherName);
        return DataBaseUtil3.commonQuery(sql, Publisher.class, publisherMap);
    }

    @Override
    public Publisher queryPublisher(long publisherId) {
        String sql = "SELECT publisher_id as id, publisher_name as name, publisher_address as address FROM publisher where publisher_id=%s;";
        sql = String.format(sql, publisherId);
        return DataBaseUtil2.commonQuery(sql, Publisher.class);
    }

    @Override
    public void updatePublisherAddress(long publisherId, String address) {
        String sql = "UPDATE publisher set publisher_address='%s' where publisher_id=%s;";
        sql = String.format(sql, address, publisherId);
        DataBaseUtil.modify(sql);
    }

    @Override
    public void deletePubliser(long publisherId) {

    }

    @Override
    public void addBook(String bookName, String author, String price, long publisherId) {
        String sql = "insert into book(book_name, author, price, publisher_id) values('%s', '%s', '%s', %s)";
        sql = String.format(sql, bookName, author, price, publisherId);
        DataBaseUtil.modify(sql);
    }

    @Override
    public Book queryBook(long bookId) {
        String sql = "SELECT book_id as id, book_name as bookName, author as author, price as price, publisher_id as publisherId \n" +
                "from book where book_id=%s;";
        sql = String.format(sql, bookId);
        return DataBaseUtil2.commonQuery(sql, Book.class);
    }

    @Override
    public Book queryBook(String bookName) {
        String sql = "SELECT * from book where book_name='%s';";
        sql = String.format(sql, bookName);
        return DataBaseUtil.queryBook(sql);
    }

    @Override
    public List<Book> listBook(long publisherId) {
        String sql = "SELECT book_id as id, book_name as bookName, author as author, price as price, publisher_id as publisherId from book where publisher_id=%s;";
        sql = String.format(sql, publisherId);
        return DataBaseUtil2.commonQueryList(sql, Book.class);
    }

    @Override
    public int countBook(long publisherId) {
        String sql = "SELECT count(*) from book WHERE publisher_id=%s;";
        sql = String.format(sql, publisherId);
        return DataBaseUtil.count(sql);
    }

    @Override
    public void upateBookPrice(long bookId, String newPrice) {
        String sql = "UPDATE book set price=%s where book_id=%s;";
        sql = String.format(sql, newPrice, bookId);
        DataBaseUtil.modify(sql);
    }

    @Override
    public void deleteBook(long bookId) {

    }
}
